# 导包
import unittest
from api.login import LoginAPI
from api.staff import StaffAPI
from common.common_assert import common_assert
from common.dbutils import DBUtil


# 创建测试类
class TestStaffAPI(unittest.TestCase):
    # 登录状态
    TOKEN = None
    ID = None

    # 前置处理
    @classmethod
    def setUpClass(cls) -> None:
        # 删除员工添加记录
        print(DBUtil().exe_sql("DELETE from bs_user where mobile = '13285674455';"))

    # 后置处理
    @classmethod
    def tearDownClass(cls) -> None:
        # 删除员工添加记录
        print(DBUtil().exe_sql("DELETE from bs_user where mobile = '13285674455';"))

    # 登录成功
    def test00_login_success(self):
        login_data = {
            "mobile": "13800000002",
            "password": "123456"
        }
        response = LoginAPI().login(login_data)
        print(response.json())
        print(response.json().get("data"))
        # common_assert(self, response, 200, True, 10000, "成功")
        common_assert(self, response)
        # 提取登录状态保存在类的属性
        TestStaffAPI.TOKEN = response.json().get("data")
        print(TestStaffAPI.TOKEN)

    # 添加员工
    def test01_add_user(self):
        add_data = {
            "username": "jack0709t2",
            "mobile": "13285674455",
            "timeOfEntry": "2020-07-09",
            "formOfEmployment": 1,
            "workNumber": "10086",
            "departmentName": "销售",
            "departmentId": "1266699057968001024",
            "correctionTime": "2020-07-30T16:00:00.000Z"
        }
        response = StaffAPI().add_staff(add_data, TestStaffAPI.TOKEN)
        print(response.json())
        # common_assert(self, response, 200, True, 10000, "成功")
        common_assert(self, response)

        # 提取员工ID保存至类的属性中
        TestStaffAPI.ID = response.json().get("data").get("id")
        print(TestStaffAPI.ID)

        # 调用数据库工具类 查询该记录是否存在对应的表（bs_user）
        result = DBUtil().exe_sql("select * from bs_user where mobile = '13285674455';")
        print(result)
        result_id = result[0][0]
        print(result_id)
        # 断言
        self.assertEqual(TestStaffAPI.ID, result_id)

    # 修改员工
    def test02_update_user(self):
        update_data = {
            "username": "dsb777"
        }
        response = StaffAPI().update_user(TestStaffAPI.ID, update_data, TestStaffAPI.TOKEN)
        print(response.json())
        # 断言
        common_assert(self, response)

    # 查询员工
    def test03_select_user(self):
        response = StaffAPI().select_user(TestStaffAPI.ID, TestStaffAPI.TOKEN)
        print(response.json())
        common_assert(self, response)

    # 删除员工
    def test04_delete_user(self):
        response = StaffAPI().delete_user(TestStaffAPI.ID, TestStaffAPI.TOKEN)
        print(response.json())
        common_assert(self, response)

